package com.anzixuan.lightcontroller;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.WindowDecorActionBar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {

    final String TAG = "MainActivity";

    SensorManager sensorManager;
    Sensor lightSensor;
    SensorEventListener sensorEventListener;

    TextView lightValue;
    ImageView leftArrow;
    SeekBar seekBar;
    ImageView rightArrow;
    CheckBox checkBox;

    int lightPercent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    public void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.my_actionbar_color));
        } else {
            getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.my_actionbar_color)));
        }
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
//            actionBar.setTitle("hhh");
        }



        lightValue = findViewById(R.id.light_value);
        leftArrow = findViewById(R.id.left_arrow);
        seekBar = findViewById(R.id.seekbar);
        rightArrow = findViewById(R.id.right_arrow);
        checkBox = findViewById(R.id.checkbox);

        leftArrow.setOnClickListener(this);
        rightArrow.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(this);
        checkBox.setOnCheckedChangeListener(this);
        lightPercent = (int) (getScreenBrightness(this) / 255.0 * 100);
        Log.d(TAG, "yz0924 亮度值：" + getScreenBrightness(this) + "===" + lightPercent);
        lightValue.setText("亮度值：" + lightPercent + "%");
        seekBar.setProgress(lightPercent);
    }

    @Override
    public void onClick(View view) {
        if (R.id.left_arrow == view.getId()) {
            int process = seekBar.getProgress();
            if (process < 15) {
                seekBar.setProgress(10);
                setScreenBrightness(10 * 255 / 100);
            } else {
                seekBar.setProgress(process - 5);
                setScreenBrightness((process - 5) * 255 / 100);
            }
        } else if (R.id.right_arrow == view.getId()) {
            int process = seekBar.getProgress();
            if (process > 95) {
                seekBar.setProgress(100);
                setScreenBrightness(255);
            } else {
                seekBar.setProgress(process + 5);
                setScreenBrightness((process + 5) * 255 / 100);
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (checkBox.isChecked()) {
            leftArrow.setClickable(false);
            rightArrow.setClickable(false);
            seekBar.setEnabled(false);
            // 获取系统亮度传感器
            sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
            sensorEventListener = new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent event) {
                    float sensorBrightness = event.values[0]; // 获取亮度值
                    if (sensorBrightness > 255) { // 如果亮度超过最大值，则将亮度设置为最大值
                        sensorBrightness = 255;
                        setScreenBrightness((int) sensorBrightness);
                    } else if (sensorBrightness < 25) { // 如果亮度低于最小值，则将亮度设置为最小值
                        sensorBrightness = 25;
                        setScreenBrightness((int) sensorBrightness);
                    } else { // 否则将屏幕亮度设置为当前亮度值
                        setScreenBrightness((int) sensorBrightness);
                    }
                    Log.d(TAG, "yz0924event 亮度值：" + sensorBrightness);
                    lightPercent = (int) (sensorBrightness * 100 / 255);
                    Log.d(TAG, "yz0924event 亮度值白恩比：" + lightPercent);
                    seekBar.setProgress(lightPercent);
                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int i) {

                }
            };
            sensorManager.registerListener(sensorEventListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            //关闭自动亮度
            sensorManager.unregisterListener(sensorEventListener);
            sensorManager = null;
            lightSensor = null;
            sensorEventListener = null;
            leftArrow.setClickable(true);
            rightArrow.setClickable(true);
            seekBar.setEnabled(true);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        lightValue.setText("亮度值：" + i + "%");
        setScreenBrightness(i * 255 / 100);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    public void setScreenBrightness(int brightness) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness / 255f;
        Log.d(TAG, "yz0924 设置亮度值：" + lp.screenBrightness * 255);
        getWindow().setAttributes(lp);
//        ContentResolver contentResolver = MainActivity.this.getContentResolver();
//        Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
    }

    public int getScreenBrightness(Context context) {
        int brightness = 0;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            brightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return brightness;
    }
}